package com.sdy.hospitalguard.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.DateUtil;
import com.sdy.hospitalguard.biz.dto.RegulationsDTO;
import com.sdy.hospitalguard.biz.mapper.RegulationsMapper;
import com.sdy.hospitalguard.biz.model.EscortAssociated;
import com.sdy.hospitalguard.biz.model.Regulations;
import com.sdy.hospitalguard.biz.service.RegulationsService;
import com.sdy.hospitalguard.biz.service.VisitAppointmentService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

import static com.sdy.hospitalguard.biz.constants.RegulationConstants.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzq
 * @since 2020-05-12
 */
@Slf4j
@Service
public class RegulationsServiceImpl extends BaseServiceImpl<Regulations> implements RegulationsService {
    @Autowired
    private RegulationsMapper regulationsMapper;
    @Autowired
    private VisitAppointmentService visitAppointmentService;
    @Autowired
    private EscortAssociatedServiceImpl escortAssociatedService;

    @Override
    public String getEscortAssociatedCounts() {
        return getOne(new LambdaQueryWrapper<Regulations>()
                .eq(Regulations::getPkey, ESCORTASSOCIATED_ONEPATIENT)
                .eq(Regulations::getStatus, 0)
                .eq(Regulations::getType, 0)).getPvalue();
    }

    @Override
    public String getVisitTimeLimit() {
        return getOne(new LambdaQueryWrapper<Regulations>()
                .eq(Regulations::getPkey, VISITTIME_LIMIT)
                .eq(Regulations::getStatus, 0)
                .eq(Regulations::getType, 0)).getPvalue();
    }

    @Override
    public String getVisitDayLimit() {
        return getOne(new LambdaQueryWrapper<Regulations>()
                .eq(Regulations::getPkey, VISITDAY_LIMIT)
                .eq(Regulations::getStatus, 0)
                .eq(Regulations::getType, 0)).getPvalue();
    }

    @Override
    public String getExpirationTime() {
        return getOne(new LambdaQueryWrapper<Regulations>()
                .eq(Regulations::getPkey, TEMPORARYCARD_EXPIRATIONTIME)
                .eq(Regulations::getStatus, 0)
                .eq(Regulations::getType, 0)).getPvalue();
    }

    @Override
    public String selectEscortAssociatedRule(Integer formId) {
        Regulations regulations = getOne(new LambdaQueryWrapper<Regulations>()
                .eq(Regulations::getPkey, ESCORTASSOCIATED_ONEPATIENT + "_" + formId)
                .eq(Regulations::getStatus, 0)
                .eq(Regulations::getType, 1));
        if (regulations == null) {
            return getEscortAssociatedCounts();
        }
        return regulations.getPvalue();
    }

    @Override
    public void updateEscortAssociatedRule(Integer formId, String pvalue) throws BizException{
        QueryWrapper<EscortAssociated> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_id", formId);
        wrapper.eq("flow_state", 0);
        Integer size = escortAssociatedService.list(wrapper).size();
        Assert.notTrue(size <= Integer.parseInt(pvalue), "该患者当前已有" + size +"名正在陪护，陪护人数不能少于正在陪护人数!");
        Regulations regulations = getOne(new LambdaQueryWrapper<Regulations>()
                .eq(Regulations::getPkey, ESCORTASSOCIATED_ONEPATIENT + "_" + formId)
                .eq(Regulations::getStatus, 0)
                .eq(Regulations::getType, 1));
        if (regulations == null) {
            save(new Regulations()
                    .setPkey(ESCORTASSOCIATED_ONEPATIENT + "_" + formId)
                    .setPvalue(pvalue)
                    .setDescription("患者" + formId + "可以有多少个陪护人员")
                    .setType(1)
                    .setFormId(formId)
                    .setStatus(0)
                    .setCreateTime(new Date()));
        } else {
            update(new LambdaUpdateWrapper<Regulations>()
                    .eq(Regulations::getId, regulations.getId())
                    .set(Regulations::getPvalue, pvalue));
        }
    }

    @Override
    public RegulationsDTO getAllregulations() {
        RegulationsDTO regulationsDTO = new RegulationsDTO();
        regulationsDTO.setEscortAssociatedCounts(getEscortAssociatedCounts());
        regulationsDTO.setVisitTimeLimit(getVisitTimeLimit());
        regulationsDTO.setExpirationTime(getExpirationTime());
        return regulationsDTO;
    }

    @Override
    public boolean updateAllregulations(RegulationsDTO regulationsDTO) throws BizException{
        String escortAssociatedCounts = regulationsDTO.getEscortAssociatedCounts();
        String visitTimeLimit = regulationsDTO.getVisitTimeLimit();
        String expirationTime = regulationsDTO.getExpirationTime();
        Assert.isBlank(escortAssociatedCounts, "陪护人数不能为空!");
        Assert.isBlank(visitTimeLimit, "探视时间限制不能为空!");
        Assert.isBlank(expirationTime, "临时卡失效时间不能为空!");
        try {
            String[] limits = visitTimeLimit.split(",");
            String start = limits[0];
            String end = limits[1];
            String interval = limits[2];
            Assert.notTrue(visitAppointmentService.compareTime(visitAppointmentService.hmsAdd(DateUtil.getDate(new Date(), "yyyy-MM-dd"),
                    start, Integer.parseInt(interval)), end), "探视时长超出结束时间与开始时间的时间差，请重新设置!");
        } catch (Exception e) {
            Assert.isBlank(expirationTime, "探视时间限制的数据传输不规范!");
        }
        update(new UpdateWrapper<Regulations>()
                .eq("pkey", ESCORTASSOCIATED_ONEPATIENT)
                .eq("type", 0)
                .eq("status", 0)
                .set("pvalue", escortAssociatedCounts));
        update(new UpdateWrapper<Regulations>()
                .eq("pkey", VISITTIME_LIMIT)
                .eq("type", 0)
                .eq("status", 0)
                .set("pvalue", visitTimeLimit));
        update(new UpdateWrapper<Regulations>()
                .eq("pkey", TEMPORARYCARD_EXPIRATIONTIME)
                .eq("type", 0)
                .eq("status", 0)
                .set("pvalue", expirationTime));
        return true;
    }

}
